import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.math.BigInteger;
import static javax.swing.SwingConstants.RIGHT;
import static javax.swing.SwingConstants.WEST;

/**
 * Created by ospip on 2017/10/31.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class RadixConverter extends JFrame {
    private JTextField textDec, textBin, textOct, textHex;
    private JPanel panelMain, panelDec, panelBin, panelHex, panelOct;
    private long src;
    private BigInteger data = new BigInteger("0");
    private int textColumn = 90;
    private boolean lock = false;

    public RadixConverter() {
        super("进制转换");
        try {
            UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        panelMain = new JPanel();
        panelMain.setBorder(new EmptyBorder(20, 20, 20, 20));
        panelMain.setLayout(new GridLayout(4, 1, 20, 20));

        panelDec = new JPanel(new BorderLayout(25, 25));
        panelBin = new JPanel(new BorderLayout(25, 25));
        panelHex = new JPanel(new BorderLayout(25, 25));
        panelOct = new JPanel(new BorderLayout(25, 25));

        textBin = new JTextField(textColumn);
        textDec = new JTextField(textColumn);
        textOct = new JTextField(textColumn);
        textHex = new JTextField(textColumn);

        textDec.addCaretListener((e) -> {
            if (lock) return;
            lock = true;
            try {
                //src = Long.parseLong(textDec.getText(), 10);
                data = new BigInteger(textDec.getText(), 10);
                //System.out.println(src);
                textBin.setText(data.toString(2));
                textHex.setText(data.toString(16));
                textOct.setText(data.toString(8));
            }
            catch(NumberFormatException ex) {
                System.out.println(ex.getMessage());
            }
            finally {
                lock = false;
            }
        });

        textBin.addCaretListener((e) -> {
            if (lock) return;
            lock = true;
            try {
                data = new BigInteger(textBin.getText(), 2);
                //System.out.println(src);
                textDec.setText(data.toString(10));
                textHex.setText(data.toString(16));
                textOct.setText(data.toString(8));
            }
            catch(NumberFormatException ex) {
                System.out.println(ex.getMessage());
            }
            finally {
                lock = false;
            }
        });

        textHex.addCaretListener((e) -> {
            if (lock) return;
            lock = true;
            try {
                data = new BigInteger(textHex.getText(), 16);
                //System.out.println(src);
                textDec.setText(data.toString(10));
                textBin.setText(data.toString(2));
                textOct.setText(data.toString(8));
            }
            catch(NumberFormatException ex) {
                System.out.println(ex.getMessage());
            }
            finally {
                lock = false;
            }
        });

        textOct.addCaretListener((e) -> {
            if (lock) return;
            lock = true;
            try {
                data = new BigInteger(textOct.getText(), 8);
                //System.out.println(src);
                textDec.setText(data.toString(10));
                textBin.setText(data.toString(2));
                textHex.setText(data.toString(16));
            }
            catch(NumberFormatException ex) {
                System.out.println(ex.getMessage());
            }
            finally {
                lock = false;
            }
        });
        panelDec.add("West", new JLabel("十进制", RIGHT));
        panelDec.add("Center", textDec);
        panelBin.add("West", new JLabel("二进制", RIGHT));
        panelBin.add("Center", textBin);
        panelHex.add("West", new JLabel("十六进制", RIGHT));
        panelHex.add("Center", textHex);
        panelOct.add("West", new JLabel("八进制", RIGHT));
        panelOct.add("Center", textOct);

        panelMain.add(panelDec);
        panelMain.add(panelBin);
        panelMain.add(panelHex);
        panelMain.add(panelOct);

        this.setSize(1500, 400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.add(panelMain);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new RadixConverter();
    }
}
